//树形数据扁平化
function repeatTree(data) {
  let repeatArr = [];
  function recursion(data) {
    data.forEach((item) => {
      if (item.children) {
        recursion(item.children);
      }
      repeatArr.push({
        id: item.id,
        title: item.title,
        pid: item.pid ?? "",
      });
    });
  }
  recursion(data);
  return repeatArr;
}

//扁平数据转树形
function convertTree(repeatArr) {
  let tree = [];
  repeatArr.forEach((item) => {
    //查找是否存在父节点
    let parent = repeatArr.find((v) => item.pid === v.pid);
    if (parent) {
      if (!parent.children) {
        parent.children = [];
      }
      parent.children.push(item);
    } else {
      tree.push(item);
    }
  });
  return tree;
}
